package com.zj.album.graphy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zj.album.R;
import com.zj.album.graphy.module.PhotoInfo;
import com.zj.album.graphy.views.PhotoSelectedView;
import com.zj.album.graphy.views.IBaseRecyclerAdapter;
import com.zj.album.graphy.views.RecyclerHolder;

import java.lang.ref.WeakReference;

/**
 * Created by zhaojie on 2017/10/19.
 */

public class PhotoGraphAdapter extends IBaseRecyclerAdapter<PhotoInfo, PhotoGraphAdapter.PhotoHoder> {

    private LayoutInflater inflater;
    private PhotoSelectedView.ChangeListener listener;
    private WeakReference<PhotoGraphAdapter> wek_adapter;

    public PhotoGraphAdapter(PhotoSelectedView.ChangeListener listener) {
        super(null);
        wek_adapter = new WeakReference(this);
        this.listener = listener;
    }

    @Override
    public PhotoHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (inflater == null) inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_photo, parent, false);
        return new PhotoHoder(wek_adapter, view);
    }

    @Override
    public void onBindViewHolder(PhotoHoder holder, int position) {
        holder.initData(position);
    }

    public void release() {
        wek_adapter.clear();
        wek_adapter = null;
    }

    public static class PhotoHoder extends RecyclerHolder {
        private PhotoSelectedView psv;
        private PhotoGraphAdapter adapter;

        public PhotoHoder(WeakReference<PhotoGraphAdapter> ref_adapter, View v) {
            super(v);
            this.adapter = ref_adapter.get();
            this.psv = (PhotoSelectedView) v;
        }

        public void initData(final int position) {
            PhotoInfo info = adapter.getInfos().get(position);
            info.index = position;
            psv.init(adapter.listener);
            psv.setUri(position, info.uri);
            psv.setState(info.isSelector);
        }
    }
}
